package ControlLayer;

import ModelLayer.*;
 
import java.util.ArrayList;

/**
 * @author (Gruppe C) 
 * @version (2012.11.09)
 */
public class DvdCtr
{
    /*
     * Singleton - Only one instance of the class DvdCtr is allowed.
     */
    private static DvdCtr instance;
    private DvdCtr()
    {
        
    }
    public static DvdCtr getInstance()
    {
        if (instance == null)
        {
            instance = new DvdCtr();
        }
        return instance;
    }
    
    
    /*
     * Returns a dvd object by id.
     * 
     * @param   id          ID to search for.
     * @return              Returns a dvd containing the specified id.
     */
    public Dvd findDvd(int id)
    {
       // Search and return dvd by id.
       return DvdContainer.getInstance().findDvd(id);
    }
    
    /*
     * Creates a dvd.
     * 
     * @param   id              ID assigned to the dvd.
     * @param   title           DVD title.
     * @param   artist          DVD artist.
     * @param   releaseDate     DVD release date.
     */
    public void createDvd(String title, String artist, String releaseDate)
    {       
          //this method constructs a new dvd object 
          //and stores it in the container
          Dvd d = new Dvd(title, artist, releaseDate);
          DvdContainer.getInstance().addDvd(d);
    }
    
    /*
     * Deletes a dvd by id.
     * 
     * @param   id      Deletes a dvd by the specified id.
     * @return          Returns true if delete was successful.
     */
    public boolean deleteDvd(int id)
    {
        boolean dvdFound = false;
        
        // Search for the dvd by id.
        Dvd d = DvdContainer.getInstance().findDvd(id);
        
        if (d != null)
        {
            // Delete the dvd from the collection.
            DvdContainer.getInstance().deleteDvd(d);
            dvdFound = true;
        }
        else
        {
            dvdFound = false;
        }
        
        return dvdFound;
    }
    
    /*
     * Creates a copy of a specific dvd by id.
     * 
     * @param   id              ID of the dvd that will get copied.
     * @param   serialNr        The serial nr of the newly copied dvd.
     * @param   availableDate   Date from which the dvd is available.
     * @param   availablePrice  Price from which the dvd is available.
     */
    public void createCopy(int id, String serialNr, String availableDate, String availablePrice)
    {
        // Find the dvd by id.
        Dvd dvd = findDvd(id);
        
        if (dvd != null)
        {
            // Create a copy of the dvd.
            Specimen specimen = new Specimen(serialNr, availableDate, availablePrice);
            // Add copy to dvd list.
            dvd.createCopy(specimen);
        }
    }
    
    /*
     * Returns all dvds.
     * 
     * @return      an array list containing dvds.
     */
    public ArrayList<Dvd> listAllDvds()
    {
        return DvdContainer.getInstance().getAllDvds();
    }
    
    public Specimen findAvailableDvd(String title)
    {
        Dvd dvd = DvdContainer.getInstance().findDvd(title);
        
        return dvd.findAvailableSpecimen();
    }
}
